<template>
    <section>

        <el-table :data="dctionarys" v-loading="listLoading"  style="width: 100%;" >
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60" label="序号">
            </el-table-column>
            <el-table-column prop="sn" label="字典编号">
            </el-table-column>
            <el-table-column prop="name" label="字典名称" >
            </el-table-column>
            <el-table-column>
                <template slot="header" slot-scope="scope">
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </template>
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条分页条-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" :rules="rules" ref="ruleForm"  label-width="80px" >
                <el-form-item label="字典编号" prop="sn">
                    <el-input v-model="addForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="字典名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" >提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>
<script>

    export default {
        data() {
            return {
                rules: {
                    sn: [
                        {required: true, message: '字典名必填', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '字典编号必填', trigger: 'blur'}
                    ],

                },

                addForm:{
                    //字典类型id
                    id:'',
                    //部门名称
                    name:"",
                    //编号
                    sn:'',


                },
                //默认不弹出添加及修改模态框
                //addFormVisible:false,
                //添加修改模态框的标题显示
                //title:'',

                //部门列表
                dctionarys:[],
                //当前页
                currentPage:1,

                //每页条数
                pageSize:10,

                //总条数
                total:0,

                //显示列表加载框
                listLoading:false,
                //默认不弹出添加及修改模态框
                addFormVisible:false,

            }
        },
        methods: {


            //给添加修改提交按钮注册事件
            addSubmit(){
                console.debug(this.addForm)
                //提价from表单之前必须进行校验
                this.$refs["ruleForm"].validate((valid) => {
                    //校验成功才执行下面代码
                    console.debug(this.addForm)
                    if (valid) {
                        this.$http.post("/dectionary", this.addForm).then(res => {
                            let {success, msg} = res.data;
                            if(success){
                                //关闭模态框
                                this.addFormVisible = false;
                                //刷新界面
                                this.loadDctionarys();
                            }else{
                                this.$message.error(msg);
                            }
                        });
                    }
                })

            },

            //点击添加触发事件
            handleAdd(){
                //设置添加标题
                this.title="添加";
                //先重置
                if(this.$refs["ruleForm"]){
                    //当出现了校验规则的时候，才能进行重置(上面判断的原因)
                    this.$refs["ruleForm"].resetFields();//this.$refs.ruleForm.resetFields()
                }
                //手动清空form表单
                for(var k in this.addForm){
                    this.addForm[k] = "";
                };
                //点击添加弹出模态框
                this.addFormVisible=true;

            },
            //格式化状态
            formatState(row, column){
                return row.state == 0 ? "启用" : "禁用";
            },
            //给编辑按钮注册事件
            handleEdit(index,row){
                //修改标题
                this.title = "修改";
                //能解决大部分的属性
                for(var k in this.addForm){
                    this.addForm[k] = row[k];
                }
                //弹出窗体
                this.addFormVisible = true;


            },
            //给删除按钮注册事件
            handleDel(index,row){
                this.$confirm('确定删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//点击确定按钮，执行此函数中的内容
                    let para = { id: row.id };
                    this.$http.delete("/dectionary",{data:para}).then(res => {
                        let {success, msg} = res.data;
                        if(success){
                            //提示成功信息
                            this.$message({
                                type: 'success',
                                message: msg
                            });
                            this.loadDctionarys();
                        }else{
                            this.$message.error(msg);
                        }
                    });
                }).catch(() => {//点击取消按钮的时候，执行此函数中的内容
                });
            },

            //高级查询调用的查询部门方法
            filtersloadDepartments(){

            },

            //查询部门列表
            loadDctionarys(){
                //显示加载框
                this.listLoading = true;
                //前端传递的分页参数(当前页，每页条数)
                let param = {"currentPage": this.currentPage, "pageSize": this.pageSize};
                this.$http.patch("/dectionary",param).then(res=>{
                    //关闭加载框
                    this.listLoading = false;
                let{list,total} = res.data;
                this.total = total;
                this.dctionarys = list;

            })
            },

            //改变所选页触发该方法

            handleCurrentChange(val){
                //修改当前页
                this.currentPage = val;
                //再来分页查询
                this.loadDctionarys();
            },

        },
        mounted() {
            //加载主页就加载列表
            this.loadDctionarys();
            //加载所有的部门经理


        }
    }

</script>